package com.phone.handle;

import com.phone.*;
import com.phone.entity.Account;
import com.phone.entity.User;
import com.phone.entity.User2;
import com.phone.exception.*;
import com.phone.utils.TextUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEPhongLighting_DISTANTPeer;
import org.hibernate.Session;

import javax.jws.soap.SOAPBinding;
import java.util.Date;
import java.util.List;

/**
 * Created by tisen on 2017/7/5.
 */
public class UserHandle {
    private Session session;

    public UserHandle(Session session) {
        this.session = session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public Session getSession() {
        return session;
    }

    public User loginByPhone(String phone) throws Exception {
        List<User> users = session.createQuery("from User where phone='" + phone + "'").list();
        if (users == null || users.isEmpty())
            throw new NoBindingPhoneException();
        else
            return users.get(0);
    }

    public User loginByPhone(String phone, String password) throws Exception {
        List<User> users = session.createQuery("from User where phone='" + phone + "' and password='" + password + "'").list();
        if (users == null || users.isEmpty())
            throw new ErrorPhoneOrPasswordException();
        else
            return users.get(0);
    }

    public User loginByUsername(String username, String password) throws Exception {
        List<User> users = session.createQuery("from User where username='" + username + "' and password='" + password + "'").list();
        if (users == null || users.isEmpty())
            throw new ErrorUsernameOrPasswordException();
        else
            return users.get(0);
    }

    public User loginByWx(String wxOpenid) throws Exception {
        List<User> users = session.createQuery("from User where wxopenid='" + wxOpenid + "'").list();
        if (users == null || users.isEmpty())
            throw new NoBindingIdException();
        else
            return users.get(0);
    }

    public User loginByQQ(String qqOpenid) throws Exception {
        List<User> users = session.createQuery("from User where qqopenid='" + qqOpenid + "'").list();
        if (users == null || users.isEmpty())
            throw new NoBindingIdException();
        else
            return users.get(0);
    }

    public String registerByPhone(String phone) throws Exception {
        User user = new User();
        user.setPhone(phone);
        user.setInvitecode(InviteCode.getInviteCode());
        user.setDate(new Date());
        user.setRound(3);
        return (String) session.save(user);
    }

    public User registerByPhone1(String phone) throws Exception {
        User user = new User();
        user.setPhone(phone);
        user.setInvitecode(InviteCode.getInviteCode());
        user.setDate(new Date());
        user.setRound(3);
        user.setId((String) session.save(user));
        return user;
    }

    public User registerByWx(String wxOpenid, String nickname, String headimgurl) throws Exception {
        User user = new User();
        user.setWxopenid(wxOpenid);
        user.setNickname(nickname);
        user.setHeadimgurl(headimgurl);
        user.setInvitecode(InviteCode.getInviteCode());
        user.setDate(new Date());
        user.setRound(3);
        String id = (String) session.save(user);
        user.setId(id);
        return user;
    }

    public User registerByQq(String qqOpenid, String nickname, String headimgurl) throws Exception {
        User user = new User();
        user.setQqopenid(qqOpenid);
        user.setNickname(nickname);
        user.setHeadimgurl(headimgurl);
        user.setInvitecode(InviteCode.getInviteCode());
        user.setDate(new Date());
        user.setRound(3);
        String id = (String) session.save(user);
        user.setId(id);
        return user;
    }


    public User register(String username, String password, String phone) throws Exception {
        if (!checkUsername(username))
            throw new RepetitiveUsernameException();
        if (phone != null && !checkPhone(phone))
            throw new RepetitivePhoneException();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setInvitecode(InviteCode.getInviteCode());
        user.setDate(new Date());
        user.setRound(3);
        String id = (String) session.save(user);
        user.setId(id);
        return user;
    }


    public User call(String id, String phone) throws Exception {
        if (phone != null && !checkPhone(phone))
            throw new RepetitivePhoneException();
        User user = session.get(User.class, id);
        user.setPhone(phone);
        session.update(user);
        return user;
    }

    public User password(String username, String password, String newpassword) throws Exception {
        User user = loginByUsername(username, password);
        user.setPassword(newpassword);
        session.update(user);
        return user;
    }

    public User password(String phone, String newpassword) throws Exception {
        User user = getByPhone(phone);
        user.setPassword(newpassword);
        session.update(user);
        return user;
    }

    public User wx(String id, String wxid, String nickname, String headimgurl) throws Exception {
        User user = session.get(User.class, id);
        if (user == null) {
            throw new NoBindingIdException();
        }
        if (!checkWX(wxid)) {
            throw new RepetitiveWXIdException();
        }
        user.setWxopenid(wxid);
        if (!TextUtil.textIsEmpty(nickname))
            user.setNickname(nickname);
        if (!TextUtil.textIsEmpty(headimgurl))
            user.setHeadimgurl(headimgurl);
        session.update(user);
        return user;
    }

    public User qq(String id, String qqid, String nickname, String headimgurl) throws Exception {
        User user = session.get(User.class, id);
        if (user == null) {
            throw new NoBindingIdException();
        }
        if (!checkQQ(qqid)) {
            throw new RepetitiveQQIdException();
        }
        user.setQqopenid(qqid);
        if (!TextUtil.textIsEmpty(nickname))
            user.setNickname(nickname);
        if (!TextUtil.textIsEmpty(headimgurl))
            user.setHeadimgurl(headimgurl);
        session.update(user);
        return user;
    }

    public void round(String openid, int i) throws Exception {
        User user = session.get(User.class, openid);
        user.setRound(user.getRound() + i);
        session.update(user);
    }

    public User getById(String id) throws Exception {
        User user = session.get(User.class, id);
        if (user == null) {
            throw new NoBindingIdException();
        }
        return session.get(User.class, id);
    }

    public User getByWx(String wxOpenid) throws Exception {
        List<User> users = session.createQuery("from User where wxopenid='" + wxOpenid + "'").list();
        if (users == null || users.isEmpty())
            return null;
        else
            return users.get(0);
    }


    public User getByPhone(String phone) throws Exception {
        List<User> users = session.createQuery("from User where phone='" + phone + "'").list();
        if (users == null || users.isEmpty())
            throw new NoBindingPhoneException();
        else
            return users.get(0);
    }

    public boolean isCode(String code) throws Exception {
        List<User> users = session.createQuery("from User where invitecode='" + code + "'").list();
        if (users == null || users.isEmpty())
            return false;
        else
            return true;
    }

    public void pointAndMoney(String openid, int pointAdd, float moneyAdd) throws Exception {// 可为负数
        User user = session.get(User.class, openid);
        user.setPoint(user.getPoint() + pointAdd);
        user.setMoney(user.getMoney() + moneyAdd);
        session.update(user);
    }


    public User loginOrRegister(String phone) throws Exception {
        List<User> users = session.createQuery("from User where phone='" + phone + "'").list();
        if (users == null || users.isEmpty())
            return registerByPhone1(phone);
        else
            return users.get(0);
    }

    public String quick_login(String phone) throws Exception {
        List<Account> accounts = session.createQuery("from Account where phone='" + phone + "'").list();
        if (accounts == null || accounts.isEmpty())
            return quick_register(phone);
        else
            return accounts.get(0).getUser2().getId();
    }

    private String quick_register(String phone) {
        Account account = new Account();
        User2 user2 = new User2();
        user2.setNickname(phone);
        user2.setCode(InviteCode.getInviteCode());
        String userid = (String) session.save(user2);
        user2.setId(userid);
        account.setPhone(phone);
        account.setUser2(user2);
        account.setDate(new Date());
        session.save(account);
        return userid;
    }

    public String user_login(String username, String password) throws Exception {
        List<Account> users1 = session.createQuery("from Account where phone='" + username + "' and password='" + password + "'").list();
        if (users1 == null || users1.isEmpty()) {
            List<Account> users2 = session.createQuery("from Account where username='" + username + "' and password='" + password + "'").list();
            if (users2 == null || users2.isEmpty())
                throw new ErrorException(ErrorCode.ERROR_USERNAME_OR_PASSWORD);
            else
                return users2.get(0).getUser2().getId();

        } else
            return users1.get(0).getUser2().getId();
    }

    public String wx_login(String wx) throws Exception {
        List<Account> users = session.createQuery("from Account where wx='" + wx + "'").list();
        if (users == null || users.isEmpty())
            throw new ErrorException(ErrorCode.NOT_EXIST_WX_ID);
        else
            return users.get(0).getUser2().getId();
    }

    public String qq_login(String qq) throws Exception {
        List<Account> users = session.createQuery("from Account where qq='" + qq + "'").list();
        if (users == null || users.isEmpty())
            throw new ErrorException(ErrorCode.NOT_EXIST_QQ_ID);
        else
            return users.get(0).getUser2().getId();
    }

    public String phone_register(String phone) {
        return null;
    }

    public boolean checkPhone(String phone) throws Exception {
        List<Account> users = session.createQuery("from Account where phone='" + phone + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public boolean checkUsername(String username) throws Exception {
        List<Account> users = session.createQuery("from Account where username='" + username + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public boolean checkId(String id) throws Exception {
        User user = session.get(User.class, id);
        if (user == null) {
            return false;
        } else {
            return true;
        }
    }


    public boolean checkWX(String wx) throws Exception {
        List<User> users = session.createQuery("from User where wxopenid='" + wx + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

    public boolean checkQQ(String qq) throws Exception {
        List<User> users = session.createQuery("from User where qqopenid='" + qq + "'").list();
        if (users == null || users.isEmpty())
            return true;
        else
            return false;
    }

}
